package com.bazhua.bazo.controller;


import com.bazhua.bazo.common.utils.JwtUtil;
import com.bazhua.bazo.repository.model.AreaModel;
import com.bazhua.bazo.repository.model.ContextSearch;
import com.bazhua.bazo.repository.model.ContextServiceModel;
import com.bazhua.bazo.repository.model.SearchStreetModel;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.service.IAreaService;
import com.bazhua.bazo.service.ICommonService;
import com.bazhua.bazo.service.IContextService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

@Api(tags = "工具接口")
@RestController
@RequestMapping("/api/common")
public class CommonController {

    @Autowired
    private ICommonService commonService;

    @Autowired
    private IContextService contextService;

    @Autowired
    private IAreaService areaService;

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upLoadPicture", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult UpLoadPicture(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        InputStream stream = file.getInputStream();
        return commonService.upLoadPicture(fileName, stream);
    }

    @ApiOperation("提交服务信息--小程序")
    @RequestMapping(value = "/addContext", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult AddContext(@RequestBody ContextServiceModel model, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        model.setUserId(userId);
        return contextService.AddContext(model);
    }

    @ApiOperation("分页获取服务信息--后台")
    @RequestMapping(value = "/getContextPage", method = RequestMethod.POST)
    @ResponseBody
    public PageListResult GetContextPage(@RequestBody ContextSearch search) {
        return contextService.GetContextPage(search);
    }

    @ApiOperation("分页获取服务信息--小程序商户中心")
    @RequestMapping(value = "/getContextPageByUserId", method = RequestMethod.POST)
    @ResponseBody
    public PageListResult GetContextPageByUserId(@RequestBody ContextSearch search, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        search.setUserId(userId);
        return contextService.GetContextPage(search);
    }


    @ApiOperation("小程序-管理后台--获取全部区")
    @RequestMapping(value = "/getAllArea", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult GetAllArea() {
        return areaService.GetArea();
    }

    @ApiOperation("管理后台分页获取街道")
    @RequestMapping(value = "/getStreetPage", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult GetStreetPage(@RequestBody SearchStreetModel model) {
        return areaService.GetStreetPage(model);
    }

    @ApiOperation("小程序-根据区获取街道")
    @RequestMapping(value = "/getStreet", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult GetStreet(Long areaId) {
        return areaService.GetStreet(areaId);
    }

    @ApiOperation("管理后台-添加街道")
    @RequestMapping(value = "/addStreet", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult AddStreet(@RequestBody AreaModel model) {
        return areaService.AddStreet(model);
    }
    @ApiOperation("管理后台-删除街道")
    @RequestMapping(value = "/deleteStreet", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult DeleteStreet(Long streetId) {
        return areaService.DeleteStreet(streetId);
    }
}
